package com.edfu.core.coursemgt.persistence.impl;

import java.util.List;

import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.Query;

import com.edfu.core.coursemgt.persistence.api.ICourseOfferingMgtPersistence;
import com.edfu.core.coursemgt.persistence.entity.CourseOfferingEntity;
import com.edfu.core.coursemgt.persistence.entity.CourseOfferingInstanceEntity;
import com.edfu.core.coursemgt.persistence.entity.ICourseMgtEntity;
import com.edfu.core.coursemgt.persistence.entity.LessonEntity;
import com.edfu.core.coursemgt.persistence.entity.LessonUnitEntity;

public class CourseOfferingMgtPersistenceImpl implements ICourseOfferingMgtPersistence {
	
	private EntityManager entityManager;
	@PersistenceContext
	public void setEntityManager(EntityManager entityManager) {
		this.entityManager = entityManager;
	}
	
	@Override
	public void persist(ICourseMgtEntity entity) {
		entityManager.persist(entity);
	}
	
	@Override
	public ICourseMgtEntity merge(ICourseMgtEntity entity) {
		return entityManager.merge(entity);
	}
	
	@Override
	public CourseOfferingEntity getCourseOffering(String courseOfferingCode) {
		Query q = entityManager.createQuery("select co from CourseOfferingEntity co where code = :courseOfferingCode ");
		q.setParameter("courseOfferingCode", courseOfferingCode);
		CourseOfferingEntity coe = (CourseOfferingEntity)q.getSingleResult();
		return coe;
	}
	
	@Override
	public LessonEntity getLesson(String courseOfferingCode, String lessonName) {
		Query q = entityManager.createQuery("select e from LessonEntity e where e.name=:lessonName and e.courseOffering.code = :courseOfferingCode ");
		q.setParameter("lessonName", lessonName);
		q.setParameter("courseOfferingCode", courseOfferingCode);
		List<?> resultList = q.getResultList();
		if ( resultList == null || resultList.size() == 0)
			return null;
		else
			return (LessonEntity) q.getResultList().get(0);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<LessonEntity> getLessons(String courseOfferingCode) {
		Query q = entityManager.createQuery("select e from LessonEntity e where e.courseOffering.code = :courseOfferingCode");
		q.setParameter("courseOfferingCode", courseOfferingCode);
		return q.getResultList();
	}

	@Override
	public LessonUnitEntity getLessonUnit(String courseOfferingCode, String lessonName, String lessonUnitName) {
		Query q = entityManager.createQuery("select e from LessonUnitEntity e where e.name=:lessonUnitName and e.lesson.name = :lessonName and e.lesson.courseOffering.code =:courseOfferingCode");
		q.setParameter("lessonUnitName", lessonUnitName);
		q.setParameter("lessonName", lessonName);
		q.setParameter("courseOfferingCode", courseOfferingCode);
		List<?> resultList = q.getResultList();
		if ( resultList == null || resultList.size() == 0)
			return null;
		else
			return (LessonUnitEntity) q.getResultList().get(0);
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<LessonUnitEntity> getLessonUnits(String courseOfferingCode,
			String lessonName) {
		Query q = entityManager.createQuery("select e from LessonUnitEntity e where e.lesson.name = :lessonName and e.lesson.courseOffering.code =:courseOfferingCode");
		q.setParameter("lessonName", lessonName);
		q.setParameter("courseOfferingCode", courseOfferingCode);
		return q.getResultList();
	}

	@Override
	public void remove(ICourseMgtEntity entity) {
		entityManager.remove(entity);
		
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CourseOfferingEntity> getCourseOfferingsByAuthor(
			String author) {
		Query q = entityManager.createQuery("select coe from CourseOfferingEntity coe , in (coe.owners) as l where l.owners.owner.userId=:author");
		q.setParameter("author",author);
		return q.getResultList();
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<CourseOfferingEntity> getPublicallyAvailableCourseOfferings() {
		Query q = entityManager.createQuery("select e from CourseOfferingEntity e where e.visibility = 'PUBLIC' and e.state='AVAILABLE'");
		return q.getResultList();
	}

	@SuppressWarnings("unchecked")
	@Override
	public List<CourseOfferingInstanceEntity> getEnrolledCourseOfferings(
		String studentId) {
		Query q = entityManager.createQuery("select ce from CourseOfferingInstanceEntity ce, BatchEntity be, in (be.enrollments) as l  where ce.status='Active' and l.user.userId =:userId and ce.batch.id = be.id");
		q.setParameter("userId", studentId);
		return q.getResultList();
	}

	@Override
	public CourseOfferingInstanceEntity getCourseOfferingInstance(
			String courseOfferingCode) {
		Query q = entityManager.createQuery("select ce from CourseOfferingInstanceEntity ce where ce.status='Active' and ce.courseOffering.code = :courseOfferingCode");
		q.setParameter("courseOfferingCode", courseOfferingCode);
		if (q.getResultList().size() == 0)
			return null;
		else 
			return (CourseOfferingInstanceEntity)q.getResultList().get(0);
	}


}
